home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsfont.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  24.6 KB  |  877 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsfont.c,v 1.2.2.1 2000/11/21 00:20:28 rayjj Exp $ */
  20. /* Font operators for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gsutil.h"
  26. #include "gxfixed.h"
  27. #include "gxmatrix.h"
  28. #include "gzstate.h"        /* must precede gxdevice */
  29. #include "gxdevice.h"        /* must precede gxfont */
  30. #include "gxfont.h"
  31. #include "gxfcache.h"
  32. #include "gxpath.h"        /* for default implementation */
  33.  
  34. /* Imported procedures */
  35. void gs_purge_font_from_char_caches(P2(gs_font_dir *, const gs_font *));
  36.  
  37. /* Define the sizes of the various aspects of the font/character cache. */
  38. /*** Big memory machines ***/
  39. #define smax_LARGE 50        /* smax - # of scaled fonts */
  40. #define bmax_LARGE 500000    /* bmax - space for cached chars */
  41. #define mmax_LARGE 200        /* mmax - # of cached font/matrix pairs */
  42. #define cmax_LARGE 5000        /* cmax - # of cached chars */
  43. #define blimit_LARGE 2500    /* blimit/upper - max size of a single cached char */
  44. /*** Small memory machines ***/
  45. #define smax_SMALL 20        /* smax - # of scaled fonts */
  46. #define bmax_SMALL 25000    /* bmax - space for cached chars */
  47. #define mmax_SMALL 40        /* mmax - # of cached font/matrix pairs */
  48. #define cmax_SMALL 500        /* cmax - # of cached chars */
  49. #define blimit_SMALL 100    /* blimit/upper - max size of a single cached char */
  50.  
  51. /* Define a default procedure vector for fonts. */
  52. const gs_font_procs gs_font_procs_default = {
  53.     gs_no_define_font,        /* (actually a default) */
  54.     gs_no_make_font,        /* (actually a default) */
  55.     gs_default_font_info,
  56.     gs_default_same_font,
  57.     gs_no_encode_char,
  58.     gs_no_enumerate_glyph,
  59.     gs_default_glyph_info,
  60.     gs_no_glyph_outline,
  61.     gs_default_init_fstack,
  62.     gs_default_next_char_glyph,
  63.     gs_no_build_char,
  64.     {
  65.     0,            /* glyph_name */
  66.     0            /* known_encode */
  67.     }                /* callbacks */
  68. };
  69.  
  70. private_st_font_dir();
  71. private struct_proc_enum_ptrs(font_enum_ptrs);
  72. private struct_proc_reloc_ptrs(font_reloc_ptrs);
  73.  
  74. public_st_gs_font();
  75. public_st_gs_font_base();
  76. private_st_gs_font_ptr();
  77. public_st_gs_font_ptr_element();
  78.  
  79. /*
  80.  * Garbage collection of fonts poses some special problems.  On the one
  81.  * hand, we need to keep track of all existing base (not scaled) fonts,
  82.  * using the next/prev list whose head is the orig_fonts member of the font
  83.  * directory; on the other hand, we want these to be "weak" pointers that
  84.  * don't keep fonts in existence if the fonts aren't referenced from
  85.  * anywhere else.  We accomplish this as follows:
  86.  *
  87.  *     We don't trace through gs_font_dir.orig_fonts or gs_font.{next,prev}
  88.  * during the mark phase of the GC.
  89.  *
  90.  *     When we finalize a base gs_font, we unlink it from the list.  (A
  91.  * gs_font is a base font iff its base member points to itself.)
  92.  *
  93.  *     We *do* relocate the orig_fonts and next/prev pointers during the
  94.  * relocation phase of the GC.  */
  95.  
  96. /* Font directory GC procedures */
  97. private 
  98. ENUM_PTRS_WITH(font_dir_enum_ptrs, gs_font_dir *dir)
  99. {
  100.     /* Enumerate pointers from cached characters to f/m pairs, */
  101.     /* and mark the cached character glyphs. */
  102.     /* See gxfcache.h for why we do this here. */
  103.     uint cci = index - st_font_dir_max_ptrs;
  104.     uint offset, count;
  105.     uint tmask = dir->ccache.table_mask;
  106.  
  107.     if (cci == 0)
  108.     offset = 0, count = 1;
  109.     else if (cci == dir->enum_index + 1)
  110.     offset = dir->enum_offset + 1, count = 1;
  111.     else
  112.     offset = 0, count = cci;
  113.     for (; offset <= tmask; ++offset) {
  114.     cached_char *cc = dir->ccache.table[offset];
  115.  
  116.     if (cc != 0 && !--count) {
  117.         (*dir->ccache.mark_glyph)
  118.         (cc->code, dir->ccache.mark_glyph_data);
  119.         /****** HACK: break const.  We'll fix this someday. ******/
  120.         ((gs_font_dir *)dir)->enum_index = cci;
  121.         ((gs_font_dir *)dir)->enum_offset = offset;
  122.         ENUM_RETURN(cc_pair(cc) - cc->pair_index);
  123.     }
  124.     }
  125. }
  126. return 0;
  127. #define e1(i,elt) ENUM_PTR(i,gs_font_dir,elt);
  128. font_dir_do_ptrs(e1)
  129. #undef e1
  130. ENUM_PTRS_END
  131. private RELOC_PTRS_WITH(font_dir_reloc_ptrs, gs_font_dir *dir);
  132.     /* Relocate the pointers from cached characters to f/m pairs. */
  133.     /* See gxfcache.h for why we do this here. */
  134. {
  135.     int chi;
  136.  
  137.     for (chi = dir->ccache.table_mask; chi >= 0; --chi) {
  138.     cached_char *cc = dir->ccache.table[chi];
  139.  
  140.     if (cc != 0)
  141.         cc_set_pair_only(cc,
  142.                  (cached_fm_pair *)
  143.                  RELOC_OBJ(cc_pair(cc) - cc->pair_index) +
  144.                  cc->pair_index);
  145.     }
  146. }
  147.     /* We have to relocate the cached characters before we */
  148.     /* relocate dir->ccache.table! */
  149. RELOC_PTR(gs_font_dir, orig_fonts);
  150. #define r1(i,elt) RELOC_PTR(gs_font_dir, elt);
  151. font_dir_do_ptrs(r1)
  152. #undef r1
  153. RELOC_PTRS_END
  154.  
  155. /* GC procedures for fonts */
  156. /*
  157.  * When we finalize a base font, we unlink it from the orig_fonts list;
  158.  * when we finalize a scaled font, we unlink it from scaled_fonts.
  159.  * See above for more information.
  160.  */
  161. void
  162. gs_font_finalize(void *vptr)
  163. {
  164.     gs_font *const pfont = vptr;
  165.     gs_font **ppfirst;
  166.     gs_font *next = pfont->next;
  167.     gs_font *prev = pfont->prev;
  168.  
  169.     if_debug4('u', "[u]unlinking font 0x%lx, base=0x%lx, prev=0x%lx, next=0x%lx\n",
  170.         (ulong) pfont, (ulong) pfont->base, (ulong) prev, (ulong) next);
  171.     /* Notify clients that the font is being freed. */
  172.     gs_notify_all(&pfont->notify_list, NULL);
  173.     if (pfont->dir == 0)
  174.     ppfirst = 0;
  175.     else if (pfont->base == pfont)
  176.     ppfirst = &pfont->dir->orig_fonts;
  177.     else {
  178.     /*
  179.      * Track the number of cached scaled fonts.  Only decrement the
  180.      * count if we didn't do this already in gs_makefont.
  181.      */
  182.     if (next || prev || pfont->dir->scaled_fonts == pfont)
  183.         pfont->dir->ssize--;
  184.     ppfirst = &pfont->dir->scaled_fonts;
  185.     }
  186.     /*
  187.      * gs_purge_font may have unlinked this font already:
  188.      * don't unlink it twice.
  189.      */
  190.     if (next != 0 && next->prev == pfont)
  191.     next->prev = prev;
  192.     if (prev != 0) {
  193.     if (prev->next == pfont)
  194.         prev->next = next;
  195.     } else if (ppfirst != 0 && *ppfirst == pfont)
  196.     *ppfirst = next;
  197.     gs_notify_release(&pfont->notify_list);
  198. }
  199. private 
  200. ENUM_PTRS_WITH(font_enum_ptrs, gs_font *pfont) return ENUM_USING(st_gs_notify_list, &pfont->notify_list, sizeof(gs_notify_list_t), index - 5);
  201.     /* We don't enumerate next or prev of base fonts. */
  202.     /* See above for details. */
  203. case 0: ENUM_RETURN((pfont->base == pfont ? 0 : pfont->next));
  204. case 1: ENUM_RETURN((pfont->base == pfont ? 0 : pfont->prev));
  205. ENUM_PTR3(2, gs_font, dir, base, client_data);
  206. ENUM_PTRS_END
  207. private RELOC_PTRS_WITH(font_reloc_ptrs, gs_font *pfont);
  208. RELOC_USING(st_gs_notify_list, &pfont->notify_list, sizeof(gs_notify_list_t));
  209.     /* We *do* always relocate next and prev. */
  210.     /* Again, see above for details. */
  211. RELOC_PTR(gs_font, next);
  212. RELOC_PTR(gs_font, prev);
  213. RELOC_PTR3(gs_font, dir, base, client_data);
  214. RELOC_PTRS_END
  215.  
  216. /* Allocate a font directory */
  217. private bool
  218. cc_no_mark_glyph(gs_glyph glyph, void *ignore_data)
  219. {
  220.     return false;
  221. }
  222. gs_font_dir *
  223. gs_font_dir_alloc2(gs_memory_t * struct_mem, gs_memory_t * bits_mem)
  224. {
  225.     gs_font_dir *pdir = 0;
  226.  
  227. #if !arch_small_memory
  228. #  ifdef DEBUG
  229.     if (!gs_debug_c('.'))
  230. #  endif
  231.     {                /* Try allocating a very large cache. */
  232.     /* If this fails, allocate a small one. */
  233.     pdir = gs_font_dir_alloc2_limits(struct_mem, bits_mem,
  234.                      smax_LARGE, bmax_LARGE, mmax_LARGE,
  235.                      cmax_LARGE, blimit_LARGE);
  236.     }
  237.     if (pdir == 0)
  238. #endif
  239.     pdir = gs_font_dir_alloc2_limits(struct_mem, bits_mem,
  240.                      smax_SMALL, bmax_SMALL, mmax_SMALL,
  241.                      cmax_SMALL, blimit_SMALL);
  242.     if (pdir == 0)
  243.     return 0;
  244.     pdir->ccache.mark_glyph = cc_no_mark_glyph;
  245.     pdir->ccache.mark_glyph_data = 0;
  246.     return pdir;
  247. }
  248. gs_font_dir *
  249. gs_font_dir_alloc2_limits(gs_memory_t * struct_mem, gs_memory_t * bits_mem,
  250.              uint smax, uint bmax, uint mmax, uint cmax, uint upper)
  251. {
  252.     gs_font_dir *pdir =
  253.     gs_alloc_struct(struct_mem, gs_font_dir, &st_font_dir,
  254.             "font_dir_alloc(dir)");
  255.     int code;
  256.  
  257.     if (pdir == 0)
  258.     return 0;
  259.     code = gx_char_cache_alloc(struct_mem, bits_mem, pdir,
  260.                    bmax, mmax, cmax, upper);
  261.     if (code < 0) {
  262.     gs_free_object(struct_mem, pdir, "font_dir_alloc(dir)");
  263.     return 0;
  264.     }
  265.     pdir->orig_fonts = 0;
  266.     pdir->scaled_fonts = 0;
  267.     pdir->ssize = 0;
  268.     pdir->smax = smax;
  269.     return pdir;
  270. }
  271.  
  272. /* Allocate and minimally initialize a font. */
  273. gs_font *
  274. gs_font_alloc(gs_memory_t *mem, gs_memory_type_ptr_t pstype,
  275.           const gs_font_procs *procs, gs_font_dir *dir,
  276.           client_name_t cname)
  277. {
  278.     gs_font *pfont = gs_alloc_struct(mem, gs_font, pstype, cname);
  279.  
  280.     if (pfont == 0)
  281.     return 0;
  282.     pfont->next = pfont->prev = 0;
  283.     pfont->memory = mem;
  284.     pfont->dir = dir;
  285.     pfont->is_resource = false;
  286.     gs_notify_init(&pfont->notify_list, gs_memory_stable(mem));
  287.     pfont->id = gs_next_ids(1);
  288.     pfont->base = pfont;
  289.     pfont->client_data = 0;
  290.     /* not FontMatrix, FontType */
  291.     pfont->BitmapWidths = false;
  292.     pfont->ExactSize = pfont->InBetweenSize = pfont->TransformedChar =
  293.     fbit_use_outlines;
  294.     pfont->WMode = 0;
  295.     pfont->PaintType = 0;
  296.     pfont->StrokeWidth = 0;
  297.     pfont->procs = *procs;
  298.     /* not key_name, font_name */
  299.     return pfont;
  300. }
  301. /* Allocate and minimally initialize a base font. */
  302. gs_font_base *
  303. gs_font_base_alloc(gs_memory_t *mem, gs_memory_type_ptr_t pstype,
  304.            const gs_font_procs *procs, gs_font_dir *dir,
  305.            client_name_t cname)
  306. {
  307.     gs_font_base *pfont =
  308.     (gs_font_base *)gs_font_alloc(mem, pstype, procs, dir, cname);
  309.  
  310.     if (pfont == 0)
  311.     return 0;
  312.     pfont->FontBBox.p.x = pfont->FontBBox.p.y =
  313.     pfont->FontBBox.q.x = pfont->FontBBox.q.y = 0;
  314.     uid_set_invalid(&pfont->UID);
  315.     pfont->encoding_index = pfont->nearest_encoding_index = -1;
  316.     return pfont;
  317. }
  318.  
  319. /*
  320.  * Register/unregister a client for notification by a font.  Currently
  321.  * the clients are only notified when a font is freed.  Note that any
  322.  * such client must unregister itself when *it* is freed.
  323.  */
  324. int
  325. gs_font_notify_register(gs_font *font, gs_notify_proc_t proc, void *proc_data)
  326. {
  327.     return gs_notify_register(&font->notify_list, proc, proc_data);
  328. }
  329. int
  330. gs_font_notify_unregister(gs_font *font, gs_notify_proc_t proc, void *proc_data)
  331. {
  332.     return gs_notify_unregister(&font->notify_list, proc, proc_data);
  333. }
  334.  
  335. /* Link an element at the head of a chain. */
  336. private void
  337. font_link_first(gs_font **pfirst, gs_font *elt)
  338. {
  339.     gs_font *first = elt->next = *pfirst;
  340.  
  341.     if (first)
  342.     first->prev = elt;
  343.     elt->prev = 0;
  344.     *pfirst = elt;
  345. }
  346.  
  347. /* definefont */
  348. /* Use this only for original (unscaled) fonts! */
  349. /* Note that it expects pfont->procs.define_font to be set already. */
  350. int
  351. gs_definefont(gs_font_dir * pdir, gs_font * pfont)
  352. {
  353.     int code;
  354.  
  355.     pfont->dir = pdir;
  356.     pfont->base = pfont;
  357.     code = (*pfont->procs.define_font) (pdir, pfont);
  358.     if (code < 0) {        /* Make sure we don't try to finalize this font. */
  359.     pfont->base = 0;
  360.     return code;
  361.     }
  362.     font_link_first(&pdir->orig_fonts, pfont);
  363.     if_debug2('m', "[m]defining font 0x%lx, next=0x%lx\n",
  364.           (ulong) pfont, (ulong) pfont->next);
  365.     return 0;
  366. }
  367.  
  368. /* scalefont */
  369. int
  370. gs_scalefont(gs_font_dir * pdir, const gs_font * pfont, floatp scale,
  371.          gs_font ** ppfont)
  372. {
  373.     gs_matrix mat;
  374.  
  375.     gs_make_scaling(scale, scale, &mat);
  376.     return gs_makefont(pdir, pfont, &mat, ppfont);
  377. }
  378.  
  379. /* makefont */
  380. int
  381. gs_makefont(gs_font_dir * pdir, const gs_font * pfont,
  382.         const gs_matrix * pmat, gs_font ** ppfont)
  383. {
  384.     int code;
  385.     gs_font *prev = 0;
  386.     gs_font *pf_out = pdir->scaled_fonts;
  387.     gs_memory_t *mem = pfont->memory;
  388.     gs_matrix newmat;
  389.     bool can_cache;
  390.  
  391.     if ((code = gs_matrix_multiply(&pfont->FontMatrix, pmat, &newmat)) < 0)
  392.     return code;
  393.     /*
  394.      * Check for the font already being in the scaled font cache.
  395.      * Until version 5.97, we only cached scaled fonts if the base
  396.      * (unscaled) font had a valid UniqueID or XUID; now, we will cache
  397.      * scaled versions of any non-composite font.
  398.      */
  399. #ifdef DEBUG
  400.     if (gs_debug_c('m')) {
  401.     const gs_font_base *const pbfont = (const gs_font_base *)pfont;
  402.  
  403.     if (pfont->FontType == ft_composite)
  404.         dlprintf("[m]composite");
  405.     else if (uid_is_UniqueID(&pbfont->UID))
  406.         dlprintf1("[m]UniqueID=%ld", pbfont->UID.id);
  407.     else if (uid_is_XUID(&pbfont->UID))
  408.         dlprintf1("[m]XUID(%u)", (uint) (-pbfont->UID.id));
  409.     else
  410.         dlprintf("[m]no UID");
  411.     dprintf7(", FontType=%d,\n[m]  new FontMatrix=[%g %g %g %g %g %g]\n",
  412.          pfont->FontType,
  413.          pmat->xx, pmat->xy, pmat->yx, pmat->yy,
  414.          pmat->tx, pmat->ty);
  415.     }
  416. #endif
  417.     /*
  418.      * Don't try to cache scaled composite fonts, because of the side
  419.      * effects on FDepVector and descendant fonts that occur in makefont.
  420.      */
  421.     if (pfont->FontType != ft_composite) {
  422.     for (; pf_out != 0; prev = pf_out, pf_out = pf_out->next)
  423.         if (pf_out->FontType == pfont->FontType &&
  424.         pf_out->base == pfont->base &&
  425.         pf_out->FontMatrix.xx == newmat.xx &&
  426.         pf_out->FontMatrix.xy == newmat.xy &&
  427.         pf_out->FontMatrix.yx == newmat.yx &&
  428.         pf_out->FontMatrix.yy == newmat.yy &&
  429.         pf_out->FontMatrix.tx == newmat.tx &&
  430.         pf_out->FontMatrix.ty == newmat.ty
  431.         ) {
  432.         *ppfont = pf_out;
  433.         if_debug1('m', "[m]found font=0x%lx\n", (ulong) pf_out);
  434.         return 0;
  435.         }
  436.     can_cache = true;
  437.     } else
  438.     can_cache = false;
  439.     pf_out = gs_alloc_struct(mem, gs_font, gs_object_type(mem, pfont),
  440.                  "gs_makefont");
  441.     if (!pf_out)
  442.     return_error(gs_error_VMerror);
  443.     memcpy(pf_out, pfont, gs_object_size(mem, pfont));
  444.     gs_notify_init(&pf_out->notify_list, gs_memory_stable(mem));
  445.     pf_out->FontMatrix = newmat;
  446.     pf_out->client_data = 0;
  447.     pf_out->dir = pdir;
  448.     pf_out->base = pfont->base;
  449.     *ppfont = pf_out;
  450.     code = (*pf_out->procs.make_font) (pdir, pfont, pmat, ppfont);
  451.     if (code < 0)
  452.     return code;
  453.     if (can_cache) {
  454.     if (pdir->ssize >= pdir->smax && prev != 0) {
  455.         /*
  456.          * We must discard a cached scaled font.
  457.          * prev points to the last (oldest) font.
  458.          * (We can't free it, because there might be
  459.          * other references to it.)
  460.          */
  461.         if_debug1('m', "[m]discarding font 0x%lx\n",
  462.               (ulong) prev);
  463.         if (prev->prev != 0)
  464.         prev->prev->next = 0;
  465.         else
  466.         pdir->scaled_fonts = 0;
  467.         pdir->ssize--;
  468.         prev->prev = 0;
  469.         if (prev->FontType != ft_composite) {
  470.         if_debug1('m', "[m]discarding UID 0x%lx\n",
  471.               (ulong) ((gs_font_base *) prev)->
  472.               UID.xvalues);
  473.         uid_free(&((gs_font_base *) prev)->UID,
  474.              prev->memory,
  475.              "gs_makefont(discarding)");
  476.         uid_set_invalid(&((gs_font_base *) prev)->UID);
  477.         }
  478.     }
  479.     pdir->ssize++;
  480.     font_link_first(&pdir->scaled_fonts, pf_out);
  481.     } else {            /* Prevent garbage pointers. */
  482.     pf_out->next = pf_out->prev = 0;
  483.     }
  484.     if_debug2('m', "[m]new font=0x%lx can_cache=%s\n",
  485.           (ulong) * ppfont, (can_cache ? "true" : "false"));
  486.     return 1;
  487. }
  488.  
  489. /* Set the current font.  This is provided only for the benefit of cshow, */
  490. /* which must reset the current font without disturbing the root font. */
  491. void
  492. gs_set_currentfont(gs_state * pgs, gs_font * pfont)
  493. {
  494.     pgs->font = pfont;
  495.     pgs->char_tm_valid = false;
  496. }
  497.  
  498. /* setfont */
  499. int
  500. gs_setfont(gs_state * pgs, gs_font * pfont)
  501. {
  502.     pgs->font = pgs->root_font = pfont;
  503.     pgs->char_tm_valid = false;
  504.     return 0;
  505. }
  506.  
  507. /* currentfont */
  508. gs_font *
  509. gs_currentfont(const gs_state * pgs)
  510. {
  511.     return pgs->font;
  512. }
  513.  
  514. /* rootfont */
  515. gs_font *
  516. gs_rootfont(const gs_state * pgs)
  517. {
  518.     return pgs->root_font;
  519. }
  520.  
  521. /* cachestatus */
  522. void
  523. gs_cachestatus(register const gs_font_dir * pdir, register uint pstat[7])
  524. {
  525.     pstat[0] = pdir->ccache.bsize;
  526.     pstat[1] = pdir->ccache.bmax;
  527.     pstat[2] = pdir->fmcache.msize;
  528.     pstat[3] = pdir->fmcache.mmax;
  529.     pstat[4] = pdir->ccache.csize;
  530.     pstat[5] = pdir->ccache.cmax;
  531.     pstat[6] = pdir->ccache.upper;
  532. }
  533.  
  534. /* setcacheparams */
  535. int
  536. gs_setcachesize(gs_font_dir * pdir, uint size)
  537. {                /* This doesn't delete anything from the cache yet. */
  538.     pdir->ccache.bmax = size;
  539.     return 0;
  540. }
  541. int
  542. gs_setcachelower(gs_font_dir * pdir, uint size)
  543. {
  544.     pdir->ccache.lower = size;
  545.     return 0;
  546. }
  547. int
  548. gs_setcacheupper(gs_font_dir * pdir, uint size)
  549. {
  550.     pdir->ccache.upper = size;
  551.     return 0;
  552. }
  553.  
  554. /* currentcacheparams */
  555. uint
  556. gs_currentcachesize(const gs_font_dir * pdir)
  557. {
  558.     return pdir->ccache.bmax;
  559. }
  560. uint
  561. gs_currentcachelower(const gs_font_dir * pdir)
  562. {
  563.     return pdir->ccache.lower;
  564. }
  565. uint
  566. gs_currentcacheupper(const gs_font_dir * pdir)
  567. {
  568.     return pdir->ccache.upper;
  569. }
  570.  
  571. /* Purge a font from all font- and character-related tables. */
  572. /* This is only used by restore (and, someday, the GC). */
  573. void
  574. gs_purge_font(gs_font * pfont)
  575. {
  576.     gs_font_dir *pdir = pfont->dir;
  577.     gs_font *pf;
  578.  
  579.     /* Remove the font from its list (orig_fonts or scaled_fonts). */
  580.     gs_font *prev = pfont->prev;
  581.     gs_font *next = pfont->next;
  582.  
  583.     if (next != 0)
  584.     next->prev = prev, pfont->next = 0;
  585.     if (prev != 0)
  586.     prev->next = next, pfont->prev = 0;
  587.     else if (pdir->orig_fonts == pfont)
  588.     pdir->orig_fonts = next;
  589.     else if (pdir->scaled_fonts == pfont)
  590.     pdir->scaled_fonts = next;
  591.     else {            /* Shouldn't happen! */
  592.     lprintf1("purged font 0x%lx not found\n", (ulong) pfont);
  593.     }
  594.  
  595.     /* Purge the font from the scaled font cache. */
  596.     for (pf = pdir->scaled_fonts; pf != 0;) {
  597.     if (pf->base == pfont) {
  598.         gs_purge_font(pf);
  599.         pf = pdir->scaled_fonts;    /* start over */
  600.     } else
  601.         pf = pf->next;
  602.     }
  603.  
  604.     /* Purge the font from the font/matrix pair cache, */
  605.     /* including all cached characters rendered with that font. */
  606.     gs_purge_font_from_char_caches(pdir, pfont);
  607.  
  608. }
  609.  
  610. /* ---------------- Default font procedures ---------------- */
  611.  
  612. /* ------ Font-level procedures ------ */
  613.  
  614. /* Default (vacuous) definefont handler. */
  615. int
  616. gs_no_define_font(gs_font_dir * pdir, gs_font * pfont)
  617. {
  618.     return 0;
  619. }
  620.  
  621. /* Default (vacuous) makefont handler. */
  622. int
  623. gs_no_make_font(gs_font_dir * pdir, const gs_font * pfont,
  624.         const gs_matrix * pmat, gs_font ** ppfont)
  625. {
  626.     return 0;
  627. }
  628. /* Makefont handler for base fonts, which must copy the XUID. */
  629. int
  630. gs_base_make_font(gs_font_dir * pdir, const gs_font * pfont,
  631.           const gs_matrix * pmat, gs_font ** ppfont)
  632. {
  633.     gs_font_base *const pbfont = (gs_font_base *)*ppfont;
  634.  
  635.     if (uid_is_XUID(&pbfont->UID)) {
  636.     uint xsize = uid_XUID_size(&pbfont->UID);
  637.     long *xvalues = (long *)
  638.         gs_alloc_byte_array(pbfont->memory, xsize, sizeof(long),
  639.                 "gs_base_make_font(XUID)");
  640.  
  641.     if (xvalues == 0)
  642.         return_error(gs_error_VMerror);
  643.     memcpy(xvalues, uid_XUID_values(&pbfont->UID),
  644.            xsize * sizeof(long));
  645.  
  646.     pbfont->UID.xvalues = xvalues;
  647.     }
  648.     return 0;
  649. }
  650.  
  651. /* Default font info procedure */
  652. int
  653. gs_default_font_info(gs_font *font, const gs_point *pscale, int members,
  654.              gs_font_info_t *info)
  655. {
  656.     int wmode = font->WMode;
  657.     gs_font_base *bfont = (gs_font_base *)font;
  658.     gs_point scale;
  659.     gs_matrix smat;
  660.     const gs_matrix *pmat;
  661.  
  662.     if (pscale == 0) {
  663.     scale.x = scale.y = 0;
  664.     pmat = 0;
  665.     } else {
  666.     scale = *pscale;
  667.     gs_make_scaling(scale.x, scale.y, &smat);
  668.     pmat = &smat;
  669.     }
  670.     info->members = 0;
  671.     if (members & FONT_INFO_FLAGS)
  672.     info->Flags_returned = 0;
  673.     if (font->FontType == ft_composite)
  674.     return 0;        /* nothing available */
  675.     if ((members & FONT_INFO_FLAGS) &&
  676.     (info->Flags_requested & FONT_IS_FIXED_WIDTH)
  677.     ) {
  678.     /*
  679.      * Scan the glyph space to compute the fixed width if any.
  680.      */
  681.     gs_glyph notdef = gs_no_glyph;
  682.     gs_glyph glyph;
  683.     int fixed_width = 0;
  684.     int index, code;
  685.  
  686.     for (index = 0;
  687.          fixed_width >= 0 &&
  688.          (code = font->procs.enumerate_glyph(font, &index, GLYPH_SPACE_NAME, &glyph)) >= 0 &&
  689.          index != 0;
  690.          ) {
  691.         gs_glyph_info_t glyph_info;
  692.         gs_const_string gnstr;
  693.  
  694.         code = font->procs.glyph_info(font, glyph, pmat,
  695.                       (GLYPH_INFO_WIDTH0 << wmode),
  696.                       &glyph_info);
  697.         if (code < 0)
  698.         return code;
  699.         if (notdef == gs_no_glyph) {
  700.         gnstr.data = (const byte *)
  701.             bfont->procs.callbacks.glyph_name(glyph, &gnstr.size);
  702.         if (gnstr.size == 7 && !memcmp(gnstr.data, ".notdef", 7)) {
  703.             notdef = glyph;
  704.             info->MissingWidth = glyph_info.width[wmode].x;
  705.             info->members |= FONT_INFO_MISSING_WIDTH;
  706.         }
  707.         }
  708.         if (glyph_info.width[wmode].y != 0)
  709.         fixed_width = min_int;
  710.         else if (fixed_width == 0)
  711.         fixed_width = glyph_info.width[wmode].x;
  712.         else if (glyph_info.width[wmode].x != fixed_width)
  713.         fixed_width = min_int;
  714.     }
  715.     if (code < 0)
  716.         return code;
  717.     if (fixed_width > 0) {
  718.         info->Flags |= FONT_IS_FIXED_WIDTH;
  719.         info->members |= FONT_INFO_AVG_WIDTH | FONT_INFO_MAX_WIDTH |
  720.         FONT_INFO_MISSING_WIDTH;
  721.         info->AvgWidth = info->MaxWidth = info->MissingWidth = fixed_width;
  722.     }
  723.     info->Flags_returned |= FONT_IS_FIXED_WIDTH;
  724.     } else if (members & FONT_INFO_MISSING_WIDTH) {
  725.     gs_glyph glyph;
  726.     int index;
  727.  
  728.     for (index = 0;
  729.          font->procs.enumerate_glyph(font, &index, GLYPH_SPACE_NAME, &glyph) >= 0 &&
  730.          index != 0;
  731.          ) {
  732.         /*
  733.          * If this is a CIDFont or TrueType font that uses integers as
  734.          * glyph names, check for glyph 0; otherwise, check for .notdef.
  735.          */
  736.         if (glyph >= gs_min_cid_glyph) {
  737.         if (glyph != gs_min_cid_glyph)
  738.             continue;
  739.         } else {
  740.         gs_const_string gnstr;
  741.  
  742.         gnstr.data = (const byte *)
  743.             bfont->procs.callbacks.glyph_name(glyph, &gnstr.size);
  744.         if (gnstr.size != 7 || memcmp(gnstr.data, ".notdef", 7))
  745.             continue;
  746.         }
  747.         {
  748.         gs_glyph_info_t glyph_info;
  749.         int code = font->procs.glyph_info(font, glyph, pmat,
  750.                           (GLYPH_INFO_WIDTH0 << wmode),
  751.                           &glyph_info);
  752.  
  753.         if (code < 0)
  754.             return code;
  755.         info->MissingWidth = glyph_info.width[wmode].x;
  756.         info->members |= FONT_INFO_MISSING_WIDTH;
  757.         break;
  758.         }
  759.     }
  760.     }
  761.     return 0;
  762. }
  763.  
  764. /* Default font similarity testing procedure */
  765. int
  766. gs_default_same_font(const gs_font *font, const gs_font *ofont, int mask)
  767. {
  768.     while (font->base != font)
  769.     font = font->base;
  770.     while (ofont->base != ofont)
  771.     ofont = ofont->base;
  772.     if (ofont == font)
  773.     return mask;
  774.     /* In general, we can't determine similarity. */
  775.     return 0;
  776. }
  777. int
  778. gs_base_same_font(const gs_font *font, const gs_font *ofont, int mask)
  779. {
  780.     int same = gs_default_same_font(font, ofont, mask);
  781.  
  782.     if (!same) {
  783.     const gs_font_base *const bfont = (const gs_font_base *)font;
  784.     const gs_font_base *const obfont = (const gs_font_base *)ofont;
  785.  
  786.     if (mask & FONT_SAME_ENCODING) {
  787.         if (bfont->encoding_index != ENCODING_INDEX_UNKNOWN ||
  788.         obfont->encoding_index != ENCODING_INDEX_UNKNOWN
  789.         ) {
  790.         if (bfont->encoding_index == obfont->encoding_index)
  791.             same |= FONT_SAME_ENCODING;
  792.         }
  793.     }
  794.     }
  795.     return same;
  796. }
  797.  
  798. /* ------ Glyph-level procedures ------ */
  799.  
  800. /* Dummy character encoding procedure */
  801. gs_glyph
  802. gs_no_encode_char(gs_font *pfont, gs_char chr, gs_glyph_space_t glyph_space)
  803. {
  804.     return gs_no_glyph;
  805. }
  806.  
  807. /* Dummy glyph enumeration procedure */
  808. int
  809. gs_no_enumerate_glyph(gs_font *font, int *pindex, gs_glyph_space_t glyph_space,
  810.               gs_glyph *pglyph)
  811. {
  812.     return_error(gs_error_undefined);
  813. }
  814.  
  815. /* Default glyph info procedure */
  816. int
  817. gs_default_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
  818.               int members, gs_glyph_info_t *info)
  819. {
  820.     gx_path *ppath = gx_path_alloc(font->memory, "glyph_path");
  821.     int returned = 0;
  822.     int code;
  823.  
  824.     if (ppath == 0)
  825.     return_error(gs_error_VMerror);
  826.     code = gx_path_add_point(ppath, fixed_0, fixed_0);
  827.     if (code < 0)
  828.     goto out;
  829.     code = font->procs.glyph_outline(font, glyph, pmat, ppath);
  830.     if (code < 0)
  831.     goto out;
  832.     if (members & GLYPH_INFO_WIDTHS) {
  833.     int wmode = font->WMode;
  834.     int wmask = GLYPH_INFO_WIDTH0 << wmode;
  835.  
  836.     if (members & wmask) {
  837.         gs_fixed_point pt;
  838.  
  839.         code = gx_path_current_point(ppath, &pt);
  840.         if (code < 0)
  841.         goto out;
  842.         info->width[wmode].x = fixed2float(pt.x);
  843.         info->width[wmode].y = fixed2float(pt.y);
  844.         returned |= wmask;
  845.     }
  846.     }
  847.     if (members & GLYPH_INFO_BBOX) {
  848.     gs_fixed_rect bbox;
  849.  
  850.     code = gx_path_bbox(ppath, &bbox);
  851.     if (code < 0)
  852.         goto out;
  853.     info->bbox.p.x = fixed2float(bbox.p.x);
  854.     info->bbox.p.y = fixed2float(bbox.p.y);
  855.     info->bbox.q.x = fixed2float(bbox.q.x);
  856.     info->bbox.q.y = fixed2float(bbox.q.y);
  857.     returned |= GLYPH_INFO_BBOX;
  858.     }
  859.     if (members & GLYPH_INFO_NUM_PIECES) {
  860.     info->num_pieces = 0;
  861.     returned |= GLYPH_INFO_NUM_PIECES;
  862.     }
  863.     returned |= members & GLYPH_INFO_PIECES; /* no pieces stored */
  864.  out:
  865.     gx_path_free(ppath, "gs_default_glyph_bbox");
  866.     info->members = returned;
  867.     return code;
  868. }
  869.  
  870. /* Dummy glyph outline procedure */
  871. int
  872. gs_no_glyph_outline(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
  873.             gx_path *ppath)
  874. {
  875.     return_error(gs_error_undefined);
  876. }
  877.